<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 标签页 -->
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-button size="small" type="primary" style="margin:20px 0" @click="addRole">新增角色</el-button>
            <!-- 表格 -->
            <el-table :data="tableData" stripe style="width: 100%">
              <!-- 增加序列号 1：给el-table-column增加type="index"属性 -->
              <!-- <el-table-column type="index" label="序号" width="100" /> -->
              <el-table-column label="序号" width="100">
                <!-- 2：作用域插槽渲染序列号 -->
                <template v-slot="{$index}">
                  <!-- <template v-slot="{$index, row}"></template> -->
                  <!-- $index是行对应得下标值 row当前行的数据对象 -->
                  <!-- 序号 = 每页展示的数量*（当前页码-1）+ 下标值+1 -->
                  {{ pagesize*(page-1)+$index +1 }}
                </template>
              </el-table-column>
              <el-table-column prop="name" label="角色" width="180" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作" width="250">
                <template v-slot="{row}">
                  <el-button type="success" size="small" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button type="primary" size="small" @click="editRole(row.id)">编辑</el-button>
                  <el-button type="danger" size="small" @click="delRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页器 -->
            <el-row type="flex" justify="center">
              <el-pagination
                :page-size="pagesize"
                layout="prev, pager, next"
                :total="50"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <!-- disabled表示禁用 -->
          <el-tab-pane label="公司信息">
            <el-form disabled label-width="120px">
              <el-form-item label="公司名称">
                <el-input v-model="Company.name" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="Company.companyAddress" />
              </el-form-item>
              <el-form-item label="公司邮箱">
                <el-input v-model="Company.mailbox" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="Company.remarks" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <AddRole
        ref="addRole"
        :show-dialog.sync="visibleDialog"
        @updateEvent="getRoleList"
      />
      <AssignPermission
        ref="assignPerm"
        :show-dialog.sync="showDialog"
      />
    </div>
  </div>
</template>

<script>
import AddRole from './componeets/addRole.vue'
import AssignPermission from './componeets/assignPermission.vue'
import { roleList, delRole, getCompany } from '@/api/setting'
export default {
  components: { AddRole, AssignPermission },
  data() {
    return {
      tableData: [],
      total: [],
      page: 1,
      pagesize: 5,
      visibleDialog: false,
      showDialog: false,
      Company: {}
    }
  },
  async created() {
    // 获取角色列表
    this.getRoleList()
    const id = this.$store.state.user.userInfo.companyId
    const res = await getCompany(id)
    // 保存公司信息
    this.Company = res.data.data
  },
  methods: {
    // 权限分配
    assignPerm(id) {
      this.showDialog = true
      // 通过ref属性，获取子组件里面的接口
      this.$refs.assignPerm.getRoleDetail(id)
    },
    // 新增角色
    addRole() {
      this.visibleDialog = true
    },
    // 删除角色
    delRole(id) {
      this.$confirm('是否确定删除？', '提示', {
        type: 'warning'
      }).then(async() => {
        // 调用接口，实现删除
        await delRole(id)
        if (this.tableData.length === 1 && this.page !== 1) {
          this.page--
        }
        // 更新列表
        this.getRoleList()
      }).catch(err => {
        console.log('点击了取消按钮:', err)
      })
    },
    // 编辑角色
    editRole(id) {
      this.visibleDialog = true
      this.$refs.addRole.getRoleDetail(id)
    },
    currentChange(page) {
      this.page = page
      this.getRoleList()
    },
    // 获取角色列表
    async getRoleList() {
      const res = await roleList({
        page: this.page,
        pagesize: this.pagesize
      })
      // 保存角色列表
      this.tableData = res.data.data.rows
      // 保存总数量
      this.total = res.data.data.total
    }
  }
}
</script>

<style>

</style>

